home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / basic / qbnws202.zip / MOUSECUR.ZIP / DIR.BAS next >
BASIC Source File  |  1991-04-25  |  3KB  |  82 lines

  1. 'DIR.BAS by Dave Cleary
  2. '
  3. 'One of the most useful additions to BASIC 7 PDS is the DIR$ function.
  4. 'This function allows you to read a directory of filenames. It also
  5. 'allows you to check the existence of a file by doing the following:
  6. '
  7. '  IF LEN(DIR$("COMMAND.COM")) THEN
  8. '     PRINT "File Found"
  9. '  ELSE
  10. '     PRINT "File not found"
  11. '  END IF
  12. '
  13. 'Now QuickBASIC 4.X users can have this useful function for their
  14. 'programs.
  15. '
  16. 'Calling DIR$ with a FileSpec$ returns the the name of the FIRST
  17. 'matching file name. Subsequent calls with a null FileSpec$ return the
  18. 'NEXT matching file name. If a null string is returned, then no more
  19. 'matching files were found. FileSpec$ can contain both a drive and a
  20. 'path plus DOS wildcards. Special care should be taken when using
  21. 'this on floppy drives because there is no check to see if the drive
  22. 'is ready.
  23.  
  24. DEFINT A-Z
  25.  
  26. DECLARE FUNCTION DIR$ (FileSpec$)
  27.  
  28. '$INCLUDE: 'QB.BI'
  29.  
  30. '-----  Some constants that DIR$ uses
  31. CONST DOS = &H21
  32. CONST SetDTA = &H1A00, FindFirst = &H4E00, FindNext = &H4F00
  33.  
  34. '--------------------------------------------------------------------
  35. 'This shows how to call DIR$ to find all matching files
  36.  
  37. 'CLS
  38. 'FileSpec$ = "C:\QB\SOURCE\*.BAS"
  39. 'Found$ = DIR$(FileSpec$)
  40. 'DO WHILE LEN(Found$)
  41. '   PRINT Found$
  42. '   Found$ = DIR$("")
  43. 'LOOP
  44.  
  45. '--------------------------------------------------------------------
  46.  
  47. FUNCTION DIR$ (FileSpec$) STATIC
  48.  
  49.    DIM DTA AS STRING * 44, Regs AS RegTypeX
  50.    Null$ = CHR$(0)
  51.  
  52. '-----  Set up our own DTA so we don't destroy COMMAND$
  53.    Regs.AX = SetDTA                    'Set DTA function
  54.    Regs.DX = VARPTR(DTA)               'DS:DX points to our DTA
  55.    Regs.DS = -1                        'Use current value for DS
  56.    InterruptX DOS, Regs, Regs          'Do the interrupt
  57.  
  58. '-----  Check to see if this is First or Next
  59.    IF LEN(FileSpec$) THEN              'FileSpec$ isn't null, so
  60.                                        'FindFirst
  61.       FileSpecZ$ = FileSpec$ + Null$   'Make FileSpec$ into an ASCIIZ
  62.                                        'string
  63.       Regs.AX = FindFirst              'Perform a FindFirst
  64.       Regs.CX = 0                      'Only look for normal files
  65.       Regs.DX = SADD(FileSpecZ$)       'DS:DX points to ASCIIZ file
  66.       Regs.DS = -1                     'Use current DS
  67.    ELSE                                'We have a null FileSpec$,
  68.       Regs.AX = FindNext               'so FindNext
  69.    END IF
  70.  
  71.    InterruptX DOS, Regs, Regs          'Do the interrupt
  72.  
  73. '-----  Return file name or null
  74.    IF Regs.Flags AND 1 THEN            'No files found
  75.       DIR$ = ""                        'Return null string
  76.    ELSE
  77.       Null = INSTR(31, DTA, Null$)     'Get the filename found
  78.       DIR$ = MID$(DTA, 31, Null - 30)  'It's an ASCIIZ string starting
  79.    END IF                              'at offset 30 of the DTA
  80.  
  81. END FUNCTION
  82.